<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here

/**
 * The Einstein Class
 */
class CMdUnit extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $unit_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $unit_code = NULL;
	var $unit_name = NULL;
	var $unit_status = NULL;
	var $unit_type = NULL;
	var $unit_active = 1;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CMdUnit() {
		$joins = array(
			array('label'=>'Unit',name=>'items', 'idfield'=>'item_id', 'joinfield'=>'item_quantity_unit'),
			array('label'=>'Unit',name=>'items', 'idfield'=>'item_id', 'joinfield'=>'item_weight_unit'),
			array('label'=>'Unit',name=>'items', 'idfield'=>'item_id', 'joinfield'=>'item_volume_unit'),
		);

		$this->CDpObject( 'md_unit', 'unit_id', 'unit_code', $joins, 'masterdata' );
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->unit_id");
		$q->addWhere("$this->_tbl_unique = '$this->unit_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate unit code: ' . $this->unit_code;
		}
		return NULL;
	}

	function getStatusName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('md_status');
		$q->addQuery('status_name');
		$q->addWhere('status_id = '.$this->unit_status);
		$sql = $q->prepare();
		$q->clear();
		$status_name = db_loadResult( $sql );
		return $status_name;
 	}

 	function getTypeName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('md_type');
		$q->addQuery('name');
		$q->addWhere('type_id = '.$this->unit_type);
		$sql = $q->prepare();
		$q->clear();
		$type_name = db_loadResult( $sql );
		return $type_name;
 	}
 	/*
 	function getStatusList(){
 		$result = array('status_id' => 0, 'status_name' => '');
		$q  = new DBQuery;
		$q->addTable('md_status');
		$q->addQuery('status_id, status_name');
		$q->addWhere('status_type =\'Unit\'');
		$sql = $q->prepare();
		$q->clear();
		//db_loadHash($sql, $result);
		return db_loadHashList($sql);
 	}
 	function getTypeList(){
 		$result = array('type_id' => 0, 'type_name' => '');
		$q  = new DBQuery;
		$q->addTable('md_type');
		$q->addQuery('type_id, name');
		$q->addWhere('type =\'Unit\'');
		$sql = $q->prepare();
		$q->clear();
		//db_loadHash($sql, $result);
		return db_loadHashList($sql);
 	}
 	*/
	function loadAllUnit($active = null){
		$q = new DBQuery;
		$q->addTable('md_unit');
		$q->addJoin('md_status', 'status', 'status_id = unit_status');
		$q->addJoin('md_type', 'type', 'type_id = unit_type');
		$q->addQuery('md_unit.*, status.status_name, type.name as type_name');
		if($active != null && $active !=''){
			$q->addWhere('unit_active = '.$active);
		}
		$q->addOrder('md_unit.unit_type');
		$psql = $q->prepare();
		$q->clear();
		return db_loadList($psql);

	}

}
function getUnitList($type=null, $required = FALSE){
	$q  = new DBQuery;
	$q->addTable('md_unit', 'u');
	$q->addJoin('md_type', 't', 't.type_id = u.unit_type');
	$q->addQuery('unit_id, unit_name');
	$q->addWhere('unit_active=1');
	if ($type != null && $type != '') {
		$q->addWhere("t.code='$type'");
	}
	$sql = $q->prepare();
	if ($required) {
		return db_loadHashList($sql);
	}
	return arrayMerge(array('' => ''), db_loadHashList($sql));
}
?>
